<template>
<div>
  <div style="margin-bottom: 20px">
    <el-input style="width:240px" placeholder="请输入历史名称" v-model="params.name"></el-input>

    <el-button style="margin-left: 5px " type="primary" @click="load"><i class="el-icon-search"></i>搜索</el-button>
    <el-button style="margin-left: 5px " type="warning" @click="reset"><i class="el-icon-refresh"></i>重置</el-button>
  </div>
  <el-table :data="tableData" stripe>
    <el-table-column prop="id" label="id"></el-table-column>
    <el-table-column prop="name" label="id"></el-table-column>
    <el-table-column prop="body" label="正文">
      <template slot-scope="scope">
        <span>{{ scope.row.body.length > 10 ? scope.row.body.substring(0, 10) + '...' : scope.row.body }}</span>
      </template>
    </el-table-column>

    <el-table-column prop="img"  label="图片">
      <template v-slot="scope">
        <el-image :src="scope.row.img" :preview-src-list="scope.row.img"></el-image>
      </template>
    </el-table-column>
    <el-table-column prop="views" label="浏览人数"></el-table-column>
    <el-table-column prop="createtime" label="创建时间"></el-table-column>


    <el-table-column label="操作">
      <template v-slot="scope">
        <!--scope就是当前行的数据-->     <!--点击跳转到这个路由，带上ID-->
        <el-button type="primary" @click="$router.push('/EditHistory?id='+scope.row.id)" >编辑</el-button>
        <el-popconfirm
            style="margin-left: 5px"
            title="确认删除这行数据吗？"
            @confirm="del(scope.row.id)"
        >
          <el-button  type="danger" slot="reference">删除</el-button>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <div style="margin-top: 10px;margin-left: 300px">
    <el-pagination
        background
        :page-size="params.pageSize"
        :current-page="params.pageNum"
        layout="prev, pager, next"
        @current-change="handleCurrentChange"
        :total="total">

    </el-pagination>
  </div>
</div>
</template>

<script>
import request from "@/util/request";
export default {
  name: "HistoryBackground",
  data(){
    return{
      tableData:[],
      params: {
        total:0,
        pageNum: 1,
        pageSize: 5,
        name:""
      }
    }
  },
  created() {
    this.load()
  },
  methods:{
    reset(){
      //重置按钮：直接把数据设置成默认
      this.params={
        pageNum:1,
        pageSize:10,
        name:"",
      }
      //再次调用查询所有
      this.load();
    },
    load(){
      request.get("/history/page",{params:this.params}).then(res=>{
        if (res.code=="200"){
          this.tableData=res.data.list
          this.total=res.data.total
        }
      })
    },
    handleCurrentChange(pageNum){
      //点击分页按钮触发
      this.params.pageNum=pageNum;
      this.load();
    },
  }
}
</script>

<style scoped>

</style>